//
//  BaseController.swift
//  SCA
//
//  Created by tjun on 2025/8/31.
//

import UIKit

class BaseController: UIViewController {
    
    var hasStatusBar: Bool {
        return true
    }

    var hiddenNavigationBar: Bool {
        return false
    }
    
    var hiddenBackButton: Bool {
        return false
    }
    
    var hiddenBackgroundView: Bool {
        return false
    }
    
    var backIsCloseButton: Bool {
        return false
    }
    
    var navigationBar: NavigationBar = {
        let navigationBar = NavigationBar()
        navigationBar.titleTextColor = Assets.Color.white
        navigationBar.backgroundColor = AppStyle.color.background
        return navigationBar
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        if (!hiddenNavigationBar) {
            view.addSubview(navigationBar)
            navigationBar.hasStatusBar = hasStatusBar

            if let title = self.title {
                navigationBar.title = title
            }
      
            if let navigationController = self.navigationController, navigationController.children.count > 1, hiddenBackButton == false {
                addBackButton()
            } else if modalPresentationStyle == .fullScreen  {
                if backIsCloseButton, !hiddenBackButton{
                    addCloseButton()
                } else if !hiddenBackButton {
                    addBackButton()
                }
            } else if let navigationController = self.navigationController, navigationController.modalPresentationStyle == .fullScreen {
                if backIsCloseButton, !hiddenBackButton {
                    addCloseButton()
                } else if !hiddenBackButton {
                    addBackButton()
                }
            }
        }
        
        view.backgroundColor = AppStyle.color.background
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        if (!hiddenNavigationBar) {
            if navigationBar.hasStatusBar {
                navigationBar.frame = CGRect(x: 0, y: 0, width: view.width, height: navBarHeight)
            } else {
                navigationBar.frame = CGRect(x: 0, y: 0, width: view.width, height: navBarHeight - statusBarHeight)
            }
            view.bringSubviewToFront(navigationBar)
        }
    }
    
    @objc func popViewController() {
        if self.navigationController == nil {
            self.dismiss(animated: true, completion: nil)
            return
        }
        
        if self.navigationController?.viewControllers.count == 1 {
            if self.presentingViewController != nil {
                self.dismiss(animated: true, completion: nil)
            } else {
                self.navigationController?.popViewController(animated: true)
            }
        } else {
            self.navigationController?.popViewController(animated: true)
        }
    }
 
    func addBackButton() {
        navigationBar.leftBarItem = BackItem.itemWithImage(Assets.Image.nav_back, target: self, action: #selector(popViewController))
    }
    
    func addCloseButton() {
        navigationBar.leftBarItem = BackItem.itemWithImage(UIImage(named: "nav_close"), target: self, action: #selector(popViewController))
        if ThemeManager.shared.currentStyle == .dark {
            navigationBar.leftBarItem?.view.tintColor = .white
        } else {
            navigationBar.leftBarItem?.view.tintColor = .black
        }
    }

}



